/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graphviewer;

import java.util.*;
import service.Storage;

/**
 *
 * @author Sabitov Artem
 * @since 21.09.2013
 */
public class Graph {
	private static int graphCount;
	private int ID;
	private ArrayList<Node> graph;
	private GraphMove move;
	
	public Graph(ArrayList<Node> graph) {
		ID = ++graphCount;
		this.graph = (ArrayList<Node>) graph.clone();
		move = new GraphMove(Storage.getViewer(), this);
		addGraphToViewer();
	}
	
	private void addGraphToViewer() {
		for(int i = 0; i < graph.size(); i++) {
			Storage.getViewer().add(graph.get(i));
		}
	}
	
	public void addNode(Node node) {
		graph.add(node);
	}
	public void delNode(Node node) {
		boolean bEnd = false;
		for(Iterator it = graph.iterator(); it.hasNext() && bEnd;) {
			Object obj = it.next();
			if(obj.equals(node)) {
				graph.remove((Node) obj);
				bEnd = true;
			}
		}
	}
	public Node getNode(int i) {
		return graph.get(i);
	}
	
	// add node with edge
	public void addNodeWE(Node node, Node nodeForEdge) {
		node.setEdge(nodeForEdge);
		graph.add(node);
	}
	
	public int getSizeGraph() {
		return graph.size();
	}
	
	public int getID() {
		return ID;
	}
	
	public void update() {
		move.update();
	}
}
